import axios from './init'
import qs from 'qs'

interface EditAttr {
  id:number,
  attrId:number,
  'attr_name':string,
  'attr_sel':string,
  'attr_vals'?:string
}

export const getAttributes = async (id:number, type:'many'|'only' = 'many') => {
  const { data } = await axios.get(`categories/${id}/attributes?${qs.stringify({ sel: type })}`)
  return data
}

export const editAttributes = async (param:EditAttr) => {
  const { data } = await axios.put(`categories/${param.id}/attributes/${param.attrId}`, {
    ...param
  })
  return data
}
export const deleteAttributes = async (param:{id:number, attrid:number}) => {
  const { data } = await axios.delete(`categories/${param.id}/attributes/${param.attrid}`)
  return data
}

export const addAttributes = async (param:{id:number, 'attr_name':string, 'attr_sel':string}) => {
  const { data } = await axios.post(`categories/${param.id}/attributes`, param)
  return data
}
